﻿namespace EyeOpen.TimeAssistant.Data.Nhibernate
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using EyeOpen.Security.Data.Nhibernate;
    using EyeOpen.TimeAssistant.Data;
    using EyeOpen.TimeAssistant.Domain;
    using EyeOpen.TimeAssistant.Queries;
    using NHibernate;
    using NHibernate.Linq;

    public class NhibernateTimeAssistantDataContext : NhibernateSecurityDataContext, ITimeAssistantDataContext
    {
        public NhibernateTimeAssistantDataContext(ISession session) 
            : base(session)
        {
        }

        public override IEnumerable<TResult> GetByQuery<TSource, TResult>(EyeOpen.Queries.IQuery<TSource, TResult> query)
        {
            if (query is ProjectNameByPriorityQuery)
            {
                return
                    (IEnumerable<TResult>)
                    Session
                        .Linq<UserStory>()
                        .Where(p => p.Priority == 1)
                        .Select(p => p.Name);
            }

            throw new NotSupportedException();
        }

        public void Add(Project project)
        {
            Save(project);
        }

        public void Add(UserStory project)
        {
            Save(project);
        }
    }
}